package com.jiayi.weiphoto.activity;

import java.io.File;
import java.io.IOException;

import com.jiayi.weiphoto.R;
import com.jiayi.weiphoto.util.DeviceTool;
import com.jiayi.weiphoto.util.FileUtil;
import com.jiayi.weiphoto.util.ImageUtil;
import com.jiayi.weiphoto.util.StringUtil;
import com.jiayi.weiphoto.util.ViewsUtil;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode.VmPolicy;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

public class AppStartGradual extends Activity{
	boolean animationFlag = false;
	boolean checkFlag = false;
	
	public static int screenWidth = 0;
	public static int screenHeight = 0;
	
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  final View view = View.inflate(this, R.layout.start, null);
		  setContentView(view);
		  screenWidth = ViewsUtil.getScreenDM(AppStartGradual.this).widthPixels;
		  screenHeight = ViewsUtil.getScreenDM(AppStartGradual.this).heightPixels;
		  AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f);
		  aa.setDuration(1000);
		  view.startAnimation(aa);
		  
		  aa.setAnimationListener(new AnimationListener() {
		   @Override
		   public void onAnimationEnd(Animation arg0) {
//			   animationFlag = true;
			   handler.sendEmptyMessageDelayed(1, 2000);
		   }
		   @Override
		   public void onAnimationRepeat(Animation animation) {
			   
		   }
		   @Override
		   public void onAnimationStart(Animation animation) {
			   
		   }
		  });
		 
		}
	
//	private void checkPicCache(){
//		ImageUtil imgUtil = ImageUtil.getInstance();
//		int len = MainActivity.imgs.length;
//		for(int i = 0; i<len; i++){
//			String picPath = StringUtil.genPicPath(i, AppStartGradual.this);
//			File picFile = new File(picPath);
//			if(!picFile.exists()){
//				Drawable drawable = getResources().getDrawable(MainActivity.imgs[i]);
//				float width = ViewsUtil.getScreenDM(AppStartGradual.this).widthPixels;
//				float insWidth = drawable.getIntrinsicWidth();
//				float scale = width / insWidth;
//				Bitmap resizeBitmap = imgUtil.resizeDrawable2Bitmap(drawable, scale, scale);
//				//imagecache+companyid/+userid/
//				FileUtil.saveBitmapToFile(resizeBitmap, StringUtil.genPicPath(i, AppStartGradual.this));
//			}
//		}
//		checkFlag = true;
//		handler.sendEmptyMessage(0);
//	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what == 0){
				if(animationFlag){
					redirectTo();
				}
			}else if(msg.what == 1){
				redirectTo();
			}
		};
	};
	
	Runnable checkImgsRuunRunnable = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
//			checkPicCache();
		}
	};
		   
		private void redirectTo() {
		  Intent intent = new Intent(this, MainActivity.class);
		  startActivity(intent);
		  finish();
		}
}
